package com.peter.spring.demo.action;

import com.peter.spring.demo.service.IModifyService;
import com.peter.spring.demo.service.IQueryService;
import com.peter.spring.framework.annotation.XDAutowired;
import com.peter.spring.framework.annotation.XDController;
import com.peter.spring.framework.annotation.XDRequestMapping;
import com.peter.spring.framework.annotation.XDRequestParam;
import com.peter.spring.framework.webmvc.servlet.XDModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: 肖冬
 * @Date: 2020/7/20 20:54
 * @Description: 公布接口url
 */
@XDController
@XDRequestMapping("/web")
public class MyAction {

    @XDAutowired
    IQueryService queryService;

    @XDAutowired
    IModifyService modifyService;

    @XDRequestMapping("/first")
    public XDModelAndView first(){
        Map<String,Object> model = new HashMap<String, Object>();
        model.put("name","peter");
        model.put("data","2020-07-22");
        model.put("token", UUID.randomUUID());
        return new XDModelAndView("first",model);
    }

    @XDRequestMapping("/query.json")
    public XDModelAndView query(HttpServletRequest request, HttpServletResponse response,
                                @XDRequestParam("name") String name){
        String result = queryService.query(name);
        return out(response,result);
    }

    @XDRequestMapping("/add*.json")
    public XDModelAndView add(HttpServletRequest request,HttpServletResponse response,
                              @XDRequestParam("name") String name,@XDRequestParam("addr") String addr){
        String result = null;
        try{
            result = modifyService.add(name,addr);
            return out(response,result);
        }catch (Exception e){
            HashMap<String, Object> model = new HashMap<String, Object>();
            model.put("detail",e.getCause().getMessage());
            model.put("stackTrace", Arrays.toString(e.getStackTrace()).replaceAll("\\[|\\]",""));
            return new XDModelAndView("500",model);
        }
    }

    @XDRequestMapping("/remove.json")
    public XDModelAndView remove(HttpServletRequest request,HttpServletResponse response,
                                 @XDRequestParam("id") Integer id){
        String result = modifyService.remove(id);
        return out(response,result);
    }

    @XDRequestMapping("/edit.json")
    public XDModelAndView edit(HttpServletRequest request,HttpServletResponse response,
                               @XDRequestParam("id") Integer id,
                               @XDRequestParam("name") String name){
        String result = modifyService.edit(id, name);
        return out(response,result);
    }


    private XDModelAndView out(HttpServletResponse response, String str) {
        try{
            response.getWriter().write(str);
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

}
